<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/5/1 0001
 * Time: 17:45
 */
namespace app\admin\model\publicuse;
use app\admin\model\traits\TablHandle;
use think\facade\Db;

class PublicUse
{
    protected $message = '';
    public function getMessage(){
        return $this->message;
    }
    /**获取
     * @param string $class
     * @return mixed|string
     */
    public static function getConfigDir($class=''){
//        $os = DIR
        $array = explode('\\',$class);
        $newdata = [];
        if(isset($array[count($array)-1])){
            $newdata['class'] = $array[count($array)-1];
        }else{
            $newdata['class'] = '';
        }
        if(isset($array[count($array)-2])){
            $newdata['model'] = $array[count($array)-2];
        }else{
            $newdata['model'] = '';
        }
        return $newdata;
    }

    /**转化字段
     * @param array $data
     * @param null $field
     * @return array
     */
    public static function Conversion($data=[],$field=null){
        if(empty($field)){
            return $data;
        }
//        return $field['shijian'];
        foreach ($field as $key=>$value){
            if($value['edit']!=1){
                continue;
            }
            if($value['formtype']=='switch'){
                if(isset($data[$key])){
                    $data[$key] = 1;
                }else{
                    $data[$key] = 0;
                }
            }elseif ($value['formtype']=='checkbox'){
                if(isset($data[$key])){
                    $str = '';
                    foreach ($data[$key] as $k=>$va){
                        $str .= $k.',';
                    }
                    $str = trim($str,',');
                    $data[$key] = $str;
                }
            }elseif ($value['formtype']=='lcheckbox'){
                if(isset($data[$key])){
                    $str = '';
                    foreach ($data[$key] as $k=>$va){
                        $str .= $va.',';
                    }
                    $str = trim($str,',');
                    $data[$key] = $str;
                }
            }elseif ($value['formtype']=='datetime'){
//                $data[$key] = strtotime($data[$key]);
                if(isset($data[$key])&&$data[$key]){
                    $data[$key] = strtotime($data[$key]);
                }
            }elseif ($value['formtype']=='editor'){
                if(isset($data[$key])&&$data[$key]){
                    $data[$key] = htmlspecialchars_decode($data[$key]);
                }
            }
        }
//        return $field;
        return $data;
    }

    /**重新获取条件
     * @param array $data
     * @return array
     */
    public static function getParameter($data=[]){
        $array = request()->param();
        foreach ($array as $key=>$value){
            if($key!=='page'&&$key!=='limit'&&$key!=='filter'&&$key!=='op'){
                if($value){
                    $data[] = [$key,'=',$value];
                }
            }
        }
        return $data;
    }

    /**获取table的字段文件
     * @param string $table
     * @return array
     */
    public static function getModelAndField($table=''){
        if(empty($table)){
            return [];
        }
        $model = Db::name('system_model')
            ->where('table',$table)
            ->findOrEmpty();
        $field = Db::name('system_field')
            ->where('table',$table)
            ->where('status',1)
            ->order('sort desc,id asc')
            ->select()->toArray();
        $newfield = [];
        foreach ($field as $value){
            if(isset($value['option'])&&$value['option']){
                $value['option'] = TablHandle::optionToArray($value['option']);
            }
            $newfield[$value['field']] = $value;
        }
        $model['field'] = $newfield;
        return $model;
    }

    /**获取模型转化
     * @param string $str
     * @return array
     */
    public static function getModeToArray($str=''){
        if(empty($str)){
            return [
                'model'=>'',
                'class'=>'',
            ];
        }
        $array = explode('_',$str);
        if(count($array)==1){
            if(empty($array[0])){
                return [
                    'model'=>'',
                    'class'=>'',
                ];
            }
            return [
                'model'=>'',
                'class'=>ucfirst($array[0]),
            ];
        }
        return [
            'model'=>$array[0],
            'class'=>ucfirst($array[1]),
        ];
    }

    /**下划线转化驼峰
     * @param string $str
     * @return string
     */
    public static function UnderlineToHump($str=''){
        if(empty($str)){
            return '';
        }
        $array = explode('_',$str);
        $newstr = '';
        foreach ($array as $key=>$value){
            if($value){
                if($key==0){
                    $newstr .= $value;
                }else{
                    $newstr .= ucfirst($value);
                }
            }
        }
        return $newstr;
    }

    /**修改数据
     * @param $obj
     * @param array $feild
     * @return mixed
     */
    public static function objctToModelAndField($obj,$feild=[],$zhuanpid=true){
        if(!is_array($obj)){
            $obj = $obj->toArray();
        }

//        return $obj;
        foreach ($obj as $key=>$value){
            foreach ($feild as $k=>$v){
                if($k=='pid'&&!$zhuanpid){
                    continue;
                }
                if($v['show']!=1){
                    continue;
                }
                if($v['formtype']=='img'){
                    if($value[$k]){
                        $value[$k] = '<img src="'.$value[$k].'" data-image="测试图片放大" style="max-height:50px;max-width:50px"/>';
                    }
                }elseif ($v['formtype']=='imgs'){
                    if($value[$k]){
                        $array = explode('|',$value[$k]);
                        $str = '';
                        foreach ($array as $vs){
                            if($vs){
                                $str .= '<img src="'.$vs.'" data-image="测试图片放大" style="max-height:100px;max-width:100px"/>';
                            }
                        }
                        $value[$k] = $str;
                    }
                }elseif ($v['formtype']=='checkbox'){
                    if($value[$k]){
                        $array = explode(',',$value[$k]);
                        $str = '';
//                        $str = json_encode($v['option']);
                        foreach ($array as $vs){
                            $str .= isset($v['option'][$vs])?($v['option'][$vs].','):'';
                        }
                        $str = trim($str,',');
                    }else{
                        $str = '';
                    }
                    $value[$k] = $str;
                }elseif ($v['formtype']=='lcheckbox'){
//                    echo '<pre>';
//                    print_r($v);
//                    exit;
                    $table = $v['join_table'];
                    $name = $v['foreign_key']??'name';
                    $shuju = Db::name($table)
                        ->where(($v['relationship_primary_key']??'id'),'in',$value[$k])
                        ->field($name)
                        ->select()->toArray();
                    $shuju = array_column($shuju,$name);
                    $value[$k] = implode(',',$shuju);
                }elseif ($v['formtype']=='color'){
                    if($value[$k]){
                        $value[$k] = '<div style="background-color: '.$value[$k].';width: 30px;height: 30px"></div>';
                    }
                }elseif ($v['formtype']=='datetime'){
                    if(!empty($value[$k])){
                        if(is_numeric($value[$k])){
                            $value[$k] = date('Y-m-d H:i:s',$value[$k]);
                        }
                    }else{
                        $value[$k] = '';
                    }
                }elseif ($v['formtype']=='lselect'||$v['formtype']=='aselect'||$v['formtype']=='selectgroup'){
                    if($value[$k]){
                        if(isset($v['join_table'])&&$v['join_table']){
                            $table = $v['join_table'];
                            $name = $v['foreign_key']??'name';
                            $shuju = Db::name($table)
                                ->where(($v['relationship_primary_key']??'id'),$value[$k])
                                ->field($v['relationship_primary_key'].','.$name)
                                ->findOrEmpty();
                            $value[$k] = $shuju[$name]??'';
                        }
                    }else{
                        $value[$k] = '';
                    }
                }elseif ($v['formtype']=='lradio'){
                    if($value[$k]){
                        if(isset($v['join_table'])&&$v['join_table']){
                            $table = $v['join_table'];
                            $name = $v['foreign_key']??'name';
                            $shuju = Db::name($table)
                                ->where(($v['relationship_primary_key']??'id'),$value[$k])
                                ->field($v['relationship_primary_key'].','.$name)
                                ->findOrEmpty();
                            $value[$k] = $shuju[$name]??'';
                        }
                    }else{
                        $value[$k] = '';
                    }
                }
            }
            $obj[$key] = $value;
        }
        return $obj;
    }

    /**修改字段
     * @param array $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function FieldGetLianJie($field=[],$row=[]){
        foreach ($field as $key=>$v){
            if($v['edit']!=1){
                continue;
            }
            if($v['formtype']=='lradio'){
                if(isset($v['join_table'])&&$v['join_table']){
                    $table = $v['join_table'];
                    $table_field = PublicUse::getModelAndField($table);
                    $name = $v['foreign_key']??'name';
                    $str = ($v['relationship_primary_key']??'id').','.$name;
                    if(isset($table_field['field']['pid'])){
                        $str .= ',pid';
                    }
                    if(isset($table_field['field']['delete_time'])){
                        $array = Db::name($table)
                            ->where('status',1)
                            ->where('delete_time',0)
                            ->field($str)
                            ->select();
                    }else{
                        $array = Db::name($table)
                            ->where('status',1)
                            ->field($str)
                            ->select();
                    }
                    $v['option'] = $array;
                }
            }elseif ($v['formtype']=='lcheckbox'){
                if(isset($v['join_table'])&&$v['join_table']){
                    $table = $v['join_table'];
                    $table_field = PublicUse::getModelAndField($table);
                    $name = $v['foreign_key']??'name';
                    $str = ($v['relationship_primary_key']??'id').','.$name;
                    if(isset($table_field['field']['pid'])){
                        $str .= ',pid';
                    }
                    if(isset($table_field['field']['delete_time'])){
                        $array = Db::name($table)
                            ->where('status',1)
                            ->where('delete_time',0)
                            ->field($str)
                            ->select();
                    }else{
                        $array = Db::name($table)
                            ->where('status',1)
                            ->field($str)
                            ->select();
                    }
                    $v['option'] = $array;
                }
            }elseif ($v['formtype']=='lselect'){
                if(isset($v['join_table'])&&$v['join_table']){
//                    echo 'haha';exit;
                    $table = $v['join_table'];
                    $table_field = PublicUse::getModelAndField($table);
                    $name = $v['foreign_key']??'name';
                    $str = ($v['relationship_primary_key']??'id').','.$name;
                    if(isset($table_field['field']['pid'])){
                        $str .= ',pid';
                    }
//                    echo $table;exit;
                    if(isset($table_field['field']['delete_time'])){
                        $array = Db::name($table)
                            ->where('status',1)
                            ->where('delete_time',0)
                            ->field($str)
                            ->select();
                    }else{
                        $array = Db::name($table)
                            ->where('status',1)
                            ->field($str)
                            ->select();
                    }
                    $v['option'] = $array;
                }
            }elseif ($v['formtype']=='selectgroup'){
                if(isset($v['join_table'])&&$v['join_table']){
//                    echo 'haha';exit;
                    $table = $v['join_table'];
                    $table_field = PublicUse::getModelAndField($table);
                    $name = $v['foreign_key']??'name';
                    $str = ($v['relationship_primary_key']??'id').','.$name;
                    if(isset($table_field['field']['pid'])){
                        $str .= ',pid';
                    }
//                    echo '<pre>';
//                    print_r($str);
//                    exit;
//                    echo $str;exit;
                    if(isset($table_field['field']['delete_time'])){
                        $array = Db::name($table)
                            ->where('status',1)
                            ->where('delete_time',0)
                            ->where('pid',0)
                            ->field($str)
                            ->select();
                    }else{
                        $array = Db::name($table)
                            ->where('status',1)
                            ->where('pid',0)
                            ->field($str)
                            ->select();
                    }
                    foreach ($array as $ks=>$vs){
                        if(isset($table_field['field']['delete_time'])){
                            $vs['child'] = Db::name($table)
                                ->where('status',1)
                                ->where('delete_time',0)
                                ->where('pid',$vs['id'])
                                ->field($str)
                                ->select();
                        }else{
                            $vs['child'] = Db::name($table)
                                ->where('status',1)
//                                ->where('delete_time',0)
                                ->where('pid',$vs['id'])
                                ->field($str)
                                ->select();
                        }
                        $array[$ks] = $vs;
                    }
//                    echo '<pre>';
//                    print_r($array);
//                    exit;
                    $v['option'] = $array;
                }
            }elseif ($v['formtype']=='aselect'){
                $table = $v['join_table'];
                $table_field = PublicUse::getModelAndField($table);
                $name = $v['foreign_key']??'name';
                $str = ($v['relationship_primary_key']??'id').','.$name;
                if(isset($table_field['field']['pid'])){
                    $str .= ',pid';
                }
//                echo '<pre>';
//                print_r($row[$v['field']]);
//                exit;
                if(isset($row[$v['field']])){
                    $array = Db::name($table)
                        ->where(($v['relationship_primary_key']??'id'),$row[$v['field']])
                        ->where('status',1)
                        ->where('delete_time',0)
                        ->field($str)
                        ->find();
//                    echo '<pre>';
//                    print_r($array);
//                    exit;
                    $v['option'] = $array;
                }else{
                    continue;
                }

            }
            $field[$key] = $v;
        }
        return $field;
    }
    public static function getPidMenuList($array,string $name='name'){
        $pidMenuList = self::buildPidMenu(0,$name, $array);
        $pidMenuList = array_merge([[
            'id'    => 0,
            'pid'   => 0,
            $name => '顶级',
        ]], $pidMenuList);
        return $pidMenuList;
    }
    protected static function buildPidMenu($pid,$name, $list, $level = 0){
        $newList = [];
        foreach ($list as $vo) {
            if ($vo['pid'] == $pid) {
                $level++;
                foreach ($newList as $v) {
                    if ($vo['pid'] == $v['pid'] && isset($v['level'])) {
                        $level = $v['level'];
                        break;
                    }
                }
                $vo['level'] = $level;
                if ($level > 1) {
//                    $repeatString = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    $repeatString = "";
                    $markString   = str_repeat("{$repeatString}├{$repeatString}", $level - 1);
                    $vo[$name]  = $markString . $vo[$name];
                }
                $newList[] = $vo;
                $childList = self::buildPidMenu($vo['id'],$name, $list, $level);
                !empty($childList) && $newList = array_merge($newList, $childList);
            }

        }
        return $newList;
    }

    /**获取当前的应用
     * @param $dir
     * @param array $auto_dir
     * @return array
     */
    public static function getdir($dir,$auto_dir=[]){
//        echo $dir;exit;
        if(empty($dir)){
            return [];
        }
        if(!is_dir($dir)){
            return [];
        }
        $array = scandir($dir);
        $newdata = [];
//        echo '<pre>';
//        print_r($array);
//        exit;
        foreach ($array as $value){
            if($value=='.'||$value=='..'){
                continue;
            }
            if(is_dir($dir.$value)&&!in_array($value,$auto_dir)){
//                echo $value;exit;
                $newdata[] = $value;
            }
        }
        return $newdata;
    }

    /**获取当前客使用的应用
     * @param string $dir
     * @return array
     */
    public static function getApp($dir=''){
        if(empty($dir)){
            $dir = base_path();
        }
        $array = self::getdir($dir,['admin','common']);
        $newarray = [];
        foreach ($array as $value){
            if(!is_file(base_path().$value.DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.'config.json')){
                continue;
            }
            try{
                $ar = json_decode(file_get_contents(base_path().$value.DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.'config.json'),true);
                if(isset($ar['install'])&&$ar['install']==1){
                    $newarray[] = $ar;
                }
            }catch (\Exception $e){
                continue;
            }
        }
        return $newarray;
    }

    /**递归删除文件
     * @param $path
     * @return bool
     */
    public static function deleteDir($path){
        if(!is_dir($path)){
            return false;
        }
        $array = scandir($path);
        foreach ($array as $value){
            if($value=='.'||$value=='..'){
                continue;
            }
            $sodir = trim($path,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$value;
            if(is_dir($sodir)){
                self::deleteDir($sodir);
                @rmdir($sodir);
            }else{
                @unlink($sodir);
            }
        }
        @rmdir($path);
        return true;
    }
    public function install($dir){

    }
}